package com.higgins.eventtracking.client.health.record;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.record.entry.RecordEntryTab;
import com.higgins.eventtracking.client.health.record.fixed.RecordFixedTab;
import com.higgins.eventtracking.client.health.record.rolling.RecordRollingTab;

public class RecordFixedRollingEntryTabs extends VerticalPanel implements GenericTabInterface {
	private TabPanel tabPanel = new TabPanel();
	private RecordFixedTab recordFixedTab = new RecordFixedTab();
	private RecordRollingTab recordRollingTab = new RecordRollingTab();
	private RecordEntryTab recordEntryTab = new RecordEntryTab();
	private boolean firstRun = true;
	private Integer tabIndex = null;
	private String tabTitle = null;
	private List<GenericTabInterface> listOfTabs = new ArrayList<GenericTabInterface>();

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}
	@Override
	public String getTabTitle()	{
		return tabTitle;
	}
	@Override
	public void setTabTitle(String tabTitle)	{
		this.tabTitle = tabTitle;
	}

	@Override
	public void update() {
		if (firstRun)	
			initialise();
		
	}
	public void initialise()	{
		populateTabPanel();
		firstRun = false;
	}
	public void populateTabPanel()	{
		addTabs();
		add(tabPanel);
	}
	private void addTabs()	{
		tabPanel.add(recordFixedTab, "Fixed");
		recordFixedTab.update();
		listOfTabs.add(recordFixedTab);
		tabPanel.selectTab(0);
		tabPanel.add(recordRollingTab, "Rolling");
		listOfTabs.add(recordRollingTab);
		tabPanel.add(recordEntryTab, "Entry");
		listOfTabs.add(recordEntryTab);
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
            	int selectedIndex = integerSelectionEvent.getSelectedItem();
            	GenericTabInterface tab = listOfTabs.get(selectedIndex);
            	if (tab!=null)	{
            		tab.update();
            	}
            }
        });
	}

	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}

}
